|
|
|
|
|
|
|
However, when you add team information, you do not supply your own key. Remember, a key is a string that uniquely identifies an object in a collection. If you don't supply your own key, Visual Basic makes one up. Therefore, the object itmX is needed to set the two sub-items. If a key were supplied, you also could have written the code as follows: |
|
|
|
|
|
|
|
|
lvMain.ListItems.Add , "RSox", "Red Sox", 1, 1
lvMain.ListItems("RSox").SubItems(1) = "64"
lvMain.ListItems("RSox").SubItems(2) = "65" |
|
|
|
 |
|
 |
|
|
Adding a ListItem Object to a ListView Control |
|
|
|
 |
|
 |
|
|
The examples in this section introduce the ListItems collection, which stores ListItem objects that are created using the Add method. The syntax for a ListItem's Add method is as follows: |
|
|
|
 |
|
 |
|
|
object.Add(index, key, text, icon, smallIcon) |
|
|
|
 |
|
 |
|
|
Here's what the different pieces are and what they do: |
|
|
|
 |
|
 |
|
|
objectFor all intents and purposes, you can use ListViewName.ListItems. This element is required. |
|
|
|
 |
|
 |
|
|
indexYou can use this number to position the ListItem object within the ListItems collection. If you don't assign a number to this argument, the ListItem is added to the end of the collection. This parameter is optional. Remember that the index of an item can change as other items are added or deleted. |
|
|
|
 |
|
 |
|
|
keyYou can use a string to give a unique, friendly name to the ListItem. This parameter is optional. |
|
|
|
 |
|
 |
|
|
textThis parameter contains the string that you want the ListItem to display in the ListView window. It is optional. However, for novice users of this control, I recommend that you use it. This argument should not be confused with the key argument. |
|
|
|
 |
|
 |
|
|
iconThis parameter contains the index number or key of the image within the ImageList that has been assigned to the Icons property of the ListView control. Use this parameter to select the image you want. This argument is optional. Be careful; if you forget to fill in a value, no large icon appears in the ListView lvwIcons view. |
|
|
|
 |
|
 |
|
|
smallIconSimilar to the previous argument, icon, this parameter contains the index number of the image within the ImageList that has been assigned to the SmallIcons property of the ListView control. It is optional. If you do not fill in a value, no small icon appears in the ListView's lvwSmallIcons, lvwList, or lvwReport views. |
|
|
|
 |
|
 |
|
|
If you want to add additional information, as done with the team win/loss information, to the created ListItem object, MyListItem, you manipulate the object's SubItems(Index) property. See your online Visual Basic documentation for additional information on the SubItems property. |
|
|
|
|
|
|
|
|
After you have data in the ListView, you can display it in several different views by setting the View property in code. To see what the different views look like, you can add a View menu to the sample program. |
|
|
|
|
|
|
|
|
To create the menu, use Visual Basic's Menu Editor (by clicking the Menu Editor button or by choosing Tools, Menu Editor), and create the menu items, as shown in Figure 12.7. |
|
|
|
|
|